﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Lifetime;
using System.Text;
using System.Threading.Tasks;

namespace Soft.Net
{
    /// <summary>
    /// 按引用封送  基类
    /// </summary>
    public abstract  class BaseRemotingObject:SoftMarshalByRefObject,IDisposable
    {


        /// <summary>
        /// 初始化生命周期服务
        /// </summary>
        /// <returns></returns>
        public override object InitializeLifetimeService()
        {
            //租赁对象的生命周期
            ILease lease = (ILease)base.InitializeLifetimeService();
            if (lease.CurrentState == LeaseState.Initial)
            {
                //初始化租赁时间 15秒
                lease.InitialLeaseTime = TimeSpan.FromSeconds(15.0);
                //获取或设置等待主办方返回租约续订时间的时间
                lease.SponsorshipTimeout = TimeSpan.FromSeconds(20.0);
                //获取或设置对远程对象的调用续订 CurrentLeaseTime 的时间
                lease.RenewOnCallTime = TimeSpan.FromSeconds(30.0);
            }
            return lease;
        }


        /// <summary>
        /// 正在执行远程过程调用
        /// </summary>
        /// <param name="items">调用参数列表</param>
        protected virtual void OnRemotingCalling(params ParameterItme[] items)
        {
            //如果正在执行远程过程调用的事件不为空
            if (this.RemotingCalling != null)
            {
                //从栈中获取方法
                MemberInfo stackMethod = baseObject.GetStackMethod(1);
                if (stackMethod != null)
                {
                    Dictionary<string, object> dictionary = new Dictionary<string, object>();
                    foreach (ParameterItme parameterItme in items)
                    {
                        dictionary.Add(parameterItme.Name, parameterItme.Value);
                    }
                    //方法名+方法参数列表
                    this.RemotingCalling(stackMethod.Name, dictionary);
                }
            }
        }


        /// <summary>
        /// 远程过程调用执行后 需要调用的方法
        /// </summary>
        protected virtual void OnRemotingCalled()
        {
            if (this.RemotingCalling != null)
            {
                MemberInfo stackMethod = baseObject.GetStackMethod(1);
                if (stackMethod != null)
                {
                    this.RemotingCalled(stackMethod.Name);
                }
            }
        }


        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnError(Exception e)
        {
            this.OnError(new Information(e));
        }


        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="error"></param>
        protected virtual void OnError(string error)
        {
            this.OnError(new Information(InformationType.Error, error));
        }


        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="error"></param>
        protected virtual void OnError(Information error)
        {
            if (this.Error != null)
            {
                this.Error(error);
            }
        }


        /// <summary>
        /// 释放
        /// </summary>
        public virtual void Dispose()
        {
            Console.WriteLine($"{this.ToString()} 被释放");
        }

   
        /// <summary>
        /// 远程对象正在被调用时的处理事件
        /// </summary>
        public event RemotingCallingEventHandle RemotingCalling;


        /// <summary>
        /// 远程对象调用后的处理事件
        /// </summary>
        public event RemotingCalledEventHandle RemotingCalled;

   
        /// <summary>
        /// 处理错误的事件
        /// </summary>
        public event ErrorEventHandle Error;




    }
}
